package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.constant.KillConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.service.IKillCourseService;
import cn.itsource.ymcc.domain.KillCourse;
import cn.itsource.ymcc.query.KillCourseQuery;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.result.PageList;
import cn.itsource.ymcc.vo.UserKillVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @GetMapping("/online/oneByOrder/{orderNo}")
    public JsonResult oneByOrder(@PathVariable("orderNo")String orderNo){
        return killCourseService.oneByOrder(orderNo);
    }

    @GetMapping("/getCourseId/{id}")
    public JsonResult oneByOrder(@PathVariable("id")Long id){
        KillCourse course_id = killCourseService.getOne(new QueryWrapper<KillCourse>().eq("course_id", id));
        return JsonResult.success().setData(JSON.toJSONString(course_id));
    }

    @PostMapping("/userKill")
    public JsonResult userKill(@RequestBody @Valid UserKillVo userKillVo){
        return killCourseService.userKill(userKillVo);
    }

    @GetMapping("/killList")
    public JsonResult killList(){
        BoundHashOperations<String, Object, Object> data = redisTemplate.boundHashOps(KillConstants.KEY_KILL_COURSE);
        List<Object> values = data.values();
        return JsonResult.success().setData(values);
    }

    @GetMapping("/killDetail/{id}")
    public JsonResult killList(@PathVariable("id")Long id){
        BoundHashOperations<String, Object, Object> data = redisTemplate.boundHashOps(KillConstants.KEY_KILL_COURSE);
        return JsonResult.success().setData(data.get(id.toString()));
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourse.setPublishStatus(RConst.Num.INT_0);
            killCourse.setCreateTime(new Date());
            killCourseService.save(killCourse);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        killCourseService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(killCourseService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(killCourseService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPageNo(), query.getPageSize());
        page = killCourseService.page(page);
        return JsonResult.success().setData(new PageList<KillCourse>(page.getTotal(), page.getRecords()));
    }
}
